//
//  ChatCell.m
//  KeyBoardTest
//
//  Created by wangqiulei on 11-8-17.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ChatCell.h"
#define KFacialSizeWidth 24
#define KFacialSizeHeight 24

@implementation ChatCell
@synthesize data;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
       
    }
    return self;
}

-(void) drawRect:(CGRect)rect
{
    CLog(@"Cell draw Rect");
    
	UIFont *fon=[UIFont systemFontOfSize:14.0f];
	CGFloat upX=5.0;
    CGFloat upY = 3.0;
	if (data) {
		for (int i=0;i<[data count];i++) {
			NSString *str=[data objectAtIndex:i];
			if ([str hasPrefix:@"<"]&&[str hasSuffix:@">"]) {
				NSString *imageName=[str substringWithRange:NSMakeRange(1, str.length-2)];
				UIImage *img=[UIImage imageNamed:imageName];
				[img drawInRect:CGRectMake(upX, upY, KFacialSizeWidth, KFacialSizeHeight)];
				upX=KFacialSizeWidth+upX;
			}else {
				CGSize size=[str sizeWithFont:fon constrainedToSize:CGSizeMake(300, 40)];
                
                while (str.length >0) {
                    if (upX + size.width >300) {
                        NSInteger length = (300.0 - upX) /(size.width /str.length);
                        NSString *drawStr = [str substringToIndex:length];
                        [drawStr drawInRect:CGRectMake(upX, upY, 300 -upX, self.bounds.size.height) withFont:fon];
                        
                        str = [str substringFromIndex:length +1];
                        upY +=16;
                        upX = 5.0;
                    }
                    else {
                        [str drawInRect:CGRectMake(upX, upY, size.width, self.bounds.size.height) withFont:fon];
                        upX=upX+size.width;
                    }
                }
                

			}

		
	}	
	}
	
}




- (void)dealloc {
	[data  release],data=nil;
    [super dealloc];
}


@end
